package com.loan.web.api;


import com.loan.entity.Perm;
import com.loan.entity.ResponseResult;
import com.loan.entity.Role;
import com.loan.service.RoleService;
import com.loan.vo.RoleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 添加角色权限表
     * @param roleVO
     * @return
     */
    @GetMapping("/add")
    public ResponseResult<Void> add(@RequestBody @Valid RoleVO roleVO){
        roleService.add(roleVO.getRole(),roleVO.getPermid());
        return ResponseResult.SUCCESS;
    }

    /**
     * 查询所有角色信息表带权限信息
     * @return
     */
//    @GetMapping("/getall")
    @RequestMapping("/getall")
    @ResponseBody
    public ResponseResult<List<Role>> getall(){
        List<Role> roleAll = roleService.getRoleAll();
        return new ResponseResult<>(roleAll);
    }

    /**
     * 查询所有角色信息，不带权限信息
     * @return
     */
    @RequestMapping("getRoleall")
    @ResponseBody
//    @GetMapping("getRoleall")
    public ResponseResult<List<Role>> getRoleall(){
        List<Role> roleall = roleService.getRoleall();
        return new ResponseResult<>(roleall);
    }

    /**
     * 修改角色表
     * @param roleVO
     * @return
     */
    @GetMapping("/update")
    public ResponseResult update(@RequestBody @Validated RoleVO roleVO){
        roleService.updateRolePerm(roleVO.getRole(),roleVO.getPermid());
        return ResponseResult.SUCCESS;
    }

    /**
     * 根据id删除角色表，逻辑删除，数据库信息改为‘n’
     * @param roleid
     * @return
     */
    @GetMapping("/delete/{roleid}")
    public ResponseResult delete(@PathVariable int roleid){
        roleService.deleteRoleId(roleid);
        return ResponseResult.SUCCESS;
    }

    /**
     * 条件查询
     * @param role
     * @return
     */
    @RequestMapping("selectbyrole")
    public ResponseResult<List<Role>> selectbyrole(@RequestBody Role role){
        List<Role> selectbyrole = roleService.selectbyrole(role);
        return new ResponseResult<>(selectbyrole);
    }
}
